package com.eke.action.goods;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;

import com.eke.bean.Goods;
import com.eke.bean.Users;
import com.eke.service.GoodsService;
import com.jason.mvc.annotations.Ajax;
import com.jason.mvc.annotations.Namespace;
import com.jason.mvc.annotations.RequestMapping;
@Namespace("/goods")
@Component("goodsAction")
@Scope(WebApplicationContext.SCOPE_REQUEST)
public class GoodsAction {
	@Resource
	private GoodsService goodsService;
	private Goods goods;
	private Users user;
	private Integer pageCount = 1;
	private Integer pageSize = 10;
	/**
	 * 新增商品
	 * @return	map
	 */
	@RequestMapping("add")
	@Ajax(true)
	public Map<String, Object> addGoods(){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", goodsService.addGoods(goods));
		return map;
	}
	
	
	/**
	 * 根据商品id查询商品
	 * @return
	 */
	@RequestMapping("queryById")
	@Ajax(true)
	public Map<String, Object> queryGoodsDetails(){
		Map<String, Object> map = new HashMap<String, Object>();
		System.out.println("11111111"+goods.getGoodsName());
		map.put("result", goodsService.queryGoods(goods.getGoodsName()));
		return map;
	}
	
	/**
	 * 根据积分兑换商品
	 * @return
	 */
	@RequestMapping("sellGoods")
	@Ajax(true)
	public Map<String, Object> sellGoods(){
		Map<String, Object> map = new HashMap<String, Object>();
		try {
			map.put("result", goodsService.sellGoods(goods.getId(), user.getUserId()));
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
		return map;
	}
	/**
	 * 根据id删除商品
	 * @return
	 */
	@RequestMapping("deleteGoods")
	@Ajax(true)
	public Map<String, Object> deleteGoods(){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", goodsService.deleteGoods(goods.getId()));
		return map;
	}
	
	/**
	 * 查询商品
	 * @param name_or_id	商品名或商品id
	 * @return
	 */
	
	@RequestMapping("queryGoods")
	@Ajax(true)
	public Map<String, Object> queryGoods(){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("result", goodsService.queryGoods(goods.getGoodsName()));
		return map;
	}
	
	/**
	 * 查询所有商品
	 * @param name_or_id	商品名或商品id
	 * @return
	 */
	@RequestMapping("queryAllGoods")
	@Ajax(true)
	public Map<String, Object> queryAllGoods(){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "success");
		map.put("count", goodsService.queryAllGoods().size());
		map.put("data", goodsService.queryAllGoods());
		return map;
	}
	
	public Integer getPageCount() {
		return pageCount < 1 ? 1 : pageCount;
	}

	public Integer getPageSize() {
		return pageSize < 0 ? 10 : pageSize;
	}
	/**
	 * 查询所有商品（分页）
	 * @return
	 */
	@RequestMapping("queryPagingGoods")
	@Ajax(true)
	public Map<String, Object> queryPagingGoods(){
		Map<String, Object> map = new HashMap<String, Object>();
		List<Goods> list = goodsService.queryPagingGoods((getPageCount()-1)*getPageSize(), getPageSize());
		map.put("code", 0);
		map.put("msg", "success");
		map.put("count", list.size());
		map.put("data", list);
		return map;
	} 
}
